package com.gqb.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.gqb.dao.BaseDao;
import com.gqb.utils.GenericsUtils;

@Transactional
@Repository
public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> {
	protected Log logger =LogFactory.getLog(BaseDaoImpl.class);
	@SuppressWarnings("unchecked")
	protected Class<T> clazz=GenericsUtils.getSuperClassGenricType(this.getClass()); ;
	
	
	

	@Override
	public void delete(int id) {
		Object object=(Object) getCSession().get(clazz, id);
		getCSession().delete(object);
	}

	@SuppressWarnings("unchecked")
	@Override
	public T getById(int id) {
		return (T)getCSession().get(clazz, id);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<T> findAll() {
		return getCSession().createQuery("from "+clazz.getSimpleName()).list();
	}
	
	@Override
	public List<T> findByIds(String[] ids) {
		
		return null;
	}

	@Override
	public int save(T entity) {
		int id=(Integer) getCSession().save(entity);
		return id;
	}
	
	@Override
	public void update(T entity) {
		getCSession().update(entity);
	}
	
	public Session getCSession(){
		return getHibernateTemplate().getSessionFactory().getCurrentSession();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<T> find(String hql) {
		
		return getHibernateTemplate().find(hql);
		
	}
	
	@Override
	public List<T> find(String hql, Object... values) {
		@SuppressWarnings("unchecked")
		List<T> lists=(List<T>)this.getHibernateTemplate().find(hql, values);
		return lists;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<T> query(String hql) {
		
		return createQuery(hql).list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<T> query(String hql, int page, int pageSize) {

		return createQuery(hql, page,pageSize).list();
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<T> query(String hql, Object... objects) {

		return createQuery(hql, objects).list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<T> pageQuery(String hql, int page, int pageSize, Object... objects) {
		return createQuery(hql, page, pageSize, objects).list();
	}
	
	public Query createQuery(String hql){
		Query query=getCSession().createQuery(hql);
		return query;
	}
	
	public Query createQuery(String hql,Object...objects){
		Query query=getCSession().createQuery(hql);
		for(int i=0;i<objects.length;i++){
			query.setParameter(i, objects[i]);
		}
		return query; 
	}
	
	public Query createQuery(String hql,int page,int PageSize){
		Query query=getCSession().createQuery(hql);
		query.setFirstResult((page-1)*PageSize);
		query.setMaxResults(PageSize);
		return query;
	}
	public Query createQuery(String hql,int page,int PageSize,Object... objects){
		Query query=getCSession().createQuery(hql);
		query.setFirstResult((page-1)*PageSize);
		query.setMaxResults(PageSize);
		for(int i=0;i<objects.length;i++){
			query.setParameter(i, objects[i]);
		}
		return query;
	}

	@Override
	public List<T> createCriteria(int page, int pageSize,
			Map<String, Object> map) {
		@SuppressWarnings("unchecked")
		List<T> list=getCSession().createCriteria(getClass())
		.add( Restrictions.allEq(map))
		.setFirstResult((page-1)*pageSize)
		.setMaxResults(pageSize).list();
		return list;
	}

	@Override
	public List<T> createCriteria(Map<String, Object> map) {
		@SuppressWarnings("unchecked")
		List<T> list=getCSession().createCriteria(getClass())
				.add(Restrictions.allEq(map))
				.list();
		return list;
	}

	@Override
	public int excudeSqlUpdate(String sql, Object... objects) {
		Query query=getCSession().createSQLQuery(sql);
		for(int i=0;i<objects.length;i++){
			query.setParameter(i, objects[i]);
		}
		return query.executeUpdate();
	}
	
	@Override
	public int update(String hql, Object... objects) {
		Query query=getCSession().createQuery(hql);
		for(int i=0;i<objects.length;i++){
			query.setParameter(i, objects[i]);
		}
		return query.executeUpdate();
	}
	
	private int getRowCount(String hql,Object...objects){
		
		return getHibernateTemplate().find(hql, objects).size();
	}

	
	
	@Override
	public int update(String hql) {
		Query query=getCSession().createQuery(hql);
		return  query.executeUpdate();
	}

	@Override
	public int excudeSql(String sql) {
		Query query=getCSession().createSQLQuery(sql);
		
		return query.executeUpdate();
	}

	@Override
	public void saveOrUpdateAll(List<T> list) {
		getHibernateTemplate().saveOrUpdateAll(list);
	}

	@Override
	public void saveOrUpdate(T entity) {
		getHibernateTemplate().saveOrUpdate(entity);
	}

	@Override
	public void  delete(T entity) {
		getHibernateTemplate().delete(entity);
	}
	
}
